package swu.myt;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ValidateCodeServlet extends HttpServlet{

	private static final long serialVersionUID = 5502595348476258099L;
	
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession(true);//session置为true
		
		ValidateCode coder =new ValidateCode();//新建一个coder对象，coder为验证码串的内容
		session.setAttribute(MyFilter.LOGIN_VALIDATE_CODE, coder.getCodeString());
		//把filter里的验证码置为刚刚新建的coder对象的内容
		response.setContentType("image/png");//设置响应内容形式为.png
		try(OutputStream output = response.getOutputStream()){
			coder.outputCodeImage(output);//输出图片
		}
	}
} 
